package us.gaaoc.framework.webapp.backingbeans.clerk.actions;

import java.util.UUID;

import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.CaseRecordStatus;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.DocumentStatus;
import us.gaaoc.framework.model.Documents;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.webapp.annotations.UserAction;

@UserAction
public class GenericImportAction {

	
	public static boolean isVendor(ModelFacade mf,String vendorId,String courtUuid)
	{
		Court court = mf.findCourtByUuid(courtUuid);	
		return court.getCmsName().equalsIgnoreCase(vendorId);
	}
	
	/**
	 * This function imports a case.
	 * @param cr The case record to send.
	 * @param sender The person who is sending.
	 * @param mf The database object.
	 * @throws GenericImportActionException 
	 */
	public void doImport(String cr,Person sender,ModelFacade mf,String court) throws GenericImportActionException
	{		
	
		CaseRecord caseRecord = mf.findCaseRecordById(cr, court);

		CaseRecordStatus newCRS = null;
		
		if(
		// we only want to enumurate cases that DON'T use web services (since it's shorter)

				!court.equalsIgnoreCase("courtUuid2") && //washington
				!court.equalsIgnoreCase("courtUuid3") && //douglas
				!court.equalsIgnoreCase("courtUuid4") && //fulton
				!court.equalsIgnoreCase("courtUuid5") && //???
				!court.equalsIgnoreCase("courtUuidOglethorpe") &&
				!court.equalsIgnoreCase("courtUuidJefferson")		
			)
		{
			if(isVendor(mf,"XMLLEGAL",court))
			{
				caseRecord.setFilingId(UUID.randomUUID().toString().replaceAll("-",""));
			}else{
				caseRecord.setFilingId(caseRecord.getUuid());
			}
			
			mf.updateCaseRecord(caseRecord, court);	
			
			newCRS = mf.findCaseRecordStatusByCode(21);
			for(Documents doc : caseRecord.getDocuments())
			{
				//If it isn't imported, go ahead
				// and queue it for import.
				if(doc.getStatus().getCode()!=2)
				{				
					mf.updateDocumentStatus(
								doc.getUuid(),
								new DocumentStatus(16,"queuedForImport"));
				}
			}
						
			// mark the case as imported
			mf.updateCaseRecordStatus(cr,newCRS,court);
			
			// Add "imported" as the case record history.
			// This is done in the webservice layer.
			//mf.addCaseRecordHistory(cr, court, 1, sender);				
		}else{
			throw new GenericImportActionException();
		}
		

	}
	
}
